cmake_minimum_required(VERSION 3.16)

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
    project(logstream VERSION 0.1.0 LANGUAGES CXX)
endif()

include(GNUInstallDirs)

if(NOT TARGET logstream)
    # Header-only interface library
    add_library(logstream INTERFACE)
    target_compile_features(logstream INTERFACE cxx_std_17)
endif()
if(NOT TARGET logstream::logstream)
    add_library(logstream::logstream ALIAS logstream)
endif()
target_include_directories(logstream INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/logstream>
)

if(NOT TARGET stlcompat)
    add_subdirectory(contrib/stlcompat)
endif()
target_link_libraries(logstream INTERFACE stlcompat::stlcompat)

# Optional example
option(LOGSTREAM_BUILD_EXAMPLES "Build logstream example program" OFF)
if(LOGSTREAM_BUILD_EXAMPLES)
    add_executable(logstream_example example.cpp)
    target_link_libraries(logstream_example PRIVATE logstream)
    # Ensure example sees headers when built standalone by CMake preset/indexers
    target_include_directories(logstream_example PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/contrib/stlcompat/include
    )
endif()

# Install header
install(FILES log_stream.h
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/logstream
)

# Export and install the interface target so downstream exports can reference it
install(TARGETS logstream
    EXPORT LogstreamTargets
)
install(EXPORT LogstreamTargets
    FILE logstreamTargets.cmake
    NAMESPACE logstream::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/logstream
)
